<?php

namespace app\controller\api\controller;

use app\support\Oss;
use app\support\ResponseHelper;
use Shayvmo\WebmanAnnotations\Annotations\PostMapping;
use Shayvmo\WebmanAnnotations\Annotations\RestController;
use Webman\Http\Request;

#[RestController("/api/common")]
class CommonController
{
    #[PostMapping("/upload")]
    public function upload(Request $request)
    {
        $file = $request->file('file');
        if (!$file) {
            return ResponseHelper::failJson("文件未上传");
        }

        try {
            $oss = new Oss();
            $objectName = "uploads/" . date("Ymd") . "/" . uniqid() . "." . $file->getUploadExtension();
            $url = $oss->upload($file->getRealPath(), $objectName);
            return ResponseHelper::successJson($url);
        } catch (\Exception $e) {
            return ResponseHelper::failJson("上传失败: " . $e->getMessage());
        }
    }
}